package com.springcloud.alibabanacosconfig.mq.listener;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rabbitmq.client.Channel;
import com.springcloud.alibabanacosconfig.mq.service.ConcurrencyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @version V1.0
 * @Author 宋聚智
 * @Date 2019/10/22 18:34
 * @description
 * @Modify By：
 * @ModifyTime： 2019/10/22
 * @Modify marker：
 **/
@Component("userOrderListener")
@Slf4j
// 为了在spring-amqp框架中进行手工确认，在接收消息时需要实现ChannelAwareMessageListener接口
public class UserOrderListener implements ChannelAwareMessageListener {
    @Autowired
    private ObjectMapper objectMapper;


    @Autowired
    ConcurrencyService concurrencyService;

    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
           long tag =message.getMessageProperties().getDeliveryTag();//消息交付的一个标识，其作用域时channel
            byte[] body=message.getBody();
            String mobile= new String(body,"UTF-8");
            concurrencyService.manageRobbing(mobile);
            log.info("监听到抢单手机号：{}",mobile);

            channel.basicAck(tag,true);//当在OnMessage方法中调用basicAct确认消息后，队列中持久化消息会被删除
        //而调用basicNack后，会收到rabbitmq重发的消息。

    }
}
